import $V from './$V';

export default function rvi(price, stdLength, length) {
    let rviValue = $V.init(price.length,50);
    let stdValue = $V.init(price.length,0);
    let diffOfPrice = $V.init(price.length,0);
    diffOfPrice = $V.copy_by_index(diffOfPrice,1,$V.sub($V.subv(price,1,price.length),$V.subv(price,0,price.length-1)));
    for(let i = stdLength - 1;i < price.length;i++){
        stdValue[i] = $V.std($V.subv(price,i-stdLength + 1,i+1));
    }
    let temp1 = $V.copy_by_index(stdValue,0,stdValue.length);
    temp1 = $V.set_by_index(temp1,$V.where(diffOfPrice,"<",0),0);
    let temp2 = $V.copy_by_index(stdValue,0,stdValue.length);
    temp2 = $V.set_by_index(temp2,$V.where(diffOfPrice,">",0),0);

    for(let j = length-1;j < price.length;j++){
        rviValue[j] = $V.sum($V.subv(temp1,j-length+1,j+1))/($V.sum($V.subv(temp1,j-length+1,j+1)) + $V.sum($V.subv(temp2,j-length+1,j+1))) * 100;
    }
    return rviValue;
}
